/**
 * @Title: MainPostsController.java
 * @Package com.zll.ccp.controller.forum
 * @author zhanglinlu
 * @date 2019年5月31日 下午2:22:45
 */
package com.zll.ccp.controller.forum;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.zll.ccp.annotation.RequireOwn;
import com.zll.ccp.model.pojo.ResourceType;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zll.ccp.model.forum.MainPosts;
import com.zll.ccp.model.forum.MinorPosts;
import com.zll.ccp.service.forum.IForumService;
import com.zll.ccp.utils.CommonUtils;

/**
 * @ClassName: MainPostsController
 * @Description: 主贴列表
 * @author zhanglinlu
 * @date 2019年5月31日 下午2:22:45
 * 
 */
@Controller
public class MainPostsController {
    @Autowired
    private IForumService forumService;

    /**
     * @Title: mainList
     * @Description: 进入主贴 列表
     * @param themeId
     * @param request
     * @return
     */
    @GetMapping("/mainpostslist")
    public String mainList(String themeId, HttpServletRequest request) {
        request.setAttribute("themeId", themeId);
        return "forum/mainlist";
    }

    /**
     * @Title: toAddMain
     * @Description: 进入增加主贴页面
     * @param themeId 主题id
     * @param request
     * @return
     */
    @GetMapping("/toaddmain")
    public String toAddMain(String themeId, HttpServletRequest request) {
        request.setAttribute("themeId", themeId);
        return "forum/addMainPosts";
    }

    /**
     * @Title: submitmainposts
     * @Description: 增加主贴
     * @param mainposts
     * @return
     */
    @PostMapping("/submitmainposts")
    @ResponseBody
    public Object submitmainposts(@RequestBody MainPosts mainposts, HttpSession session) {
        mainposts.setId(CommonUtils.getUUID());
        mainposts.setCreatetime(CommonUtils.getNowDate());
        mainposts.setUserid(session.getAttribute("userId").toString());
        forumService.addMainPosts(mainposts);
        return null;
    }

    /**
     * @Title: mainlistdata
     * @Description: 根据筛选条件获取主贴列表数据
     * @param pTitle 主贴标题
     * @param pCT 创建时间
     * @param themeId 主题类型
     * @return
     */
    @GetMapping("/mainlistdata")
    @ResponseBody
    public Object mainlistdata(String pTitle, Long pCT, String themeId) {
        if (StringUtils.isBlank(pTitle)) {
            pTitle = null;
        }
        String pct = null;
        if (0 != pCT) {
            pct = DateFormatUtils.format(pCT, "yyyy-MM-dd");
        }
        return forumService.getMainPostesByFilter(pTitle, pct, themeId);
    }

    /**
     * @Title: deleteMainPosts
     * @Description: 删除 主贴
     * @param id 主贴id
     * @return
     */
    @RequireOwn(resourceType = ResourceType.main,paramName = "id")
    @GetMapping("deletemain")
    @ResponseBody
    public Object deleteMainPosts(String id) {
        forumService.deleteMainPostsById(id);
        return null;
    }

    /**
     * @Title: topostsinfo
     * @Description:进入帖子详情
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/postsinfo")
    public String topostsinfo(String id, HttpServletRequest request) {
        request.setAttribute("id", id);
        return "forum/postsinfo";
    }

    /**
     * @Title: getMainPostsByid
     * @Description: 根据id获取主贴
     * @param id 主键id
     * @return
     */
    @GetMapping("/mainpostsbyid")
    @ResponseBody
    public Object getMainPostsByid(String id) {
        return forumService.getMainPostsById(id);
    }

    /**
     * @Title: getMinorPostsByid
     * @Description: 根据主贴id获取跟帖list
     * @param id 主贴id
     * @return
     */
    @GetMapping("/minorpostsbyid")
    @ResponseBody
    public Object getMinorPostsByid(String id) {
        return forumService.getMinorPostesByMainId(id);
    }

    /**
     * @Title: saveminorposts
     * @Description: 增加跟帖
     * @param minorPosts
     * @return
     */
    @PostMapping("/saveminorposts")
    @ResponseBody
    public Object saveminorposts(@RequestBody MinorPosts minorPosts, HttpSession session) {
        minorPosts.setId(CommonUtils.getUUID());
        minorPosts.setCreatetime(CommonUtils.getNowDate());
        minorPosts.setUserid(session.getAttribute("userId").toString());
        forumService.addMinorPosts(minorPosts);
        return null;
    }
}
